import React, { Component } from "react";
// ant
import { Modal, message } from "antd";
// API
import { getSlbDataById, updateSlbData, addSlbData } from "@/api/raycloud";
// 组件
import FormCom from "@c/form/Index";


class SlbModal extends Component {
    constructor(props){
        super(props);
        this.state = {
            isModalVisible: false,
            slb_id: "",
            formConfig: {
                url: "slbAdd",
                editKey: "",
                initValue: {
                    syncNumber: ""
                },
                setFieldValue: {},
                formatFormKey: "id"
            },
            formLayout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            },
            formItem: [
                { 
                    type: "Input",
                    label: "发布ID", 
                    name: "syncNumber", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                },
                { 
                    type: "Input",
                    label: "名称", 
                    name: "name", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                },
                { 
                    type: "Input",
                    label: "Slb IP", 
                    name: "slbIp", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                },
                { 
                    type: "Input",
                    label: "Slb实例ID", 
                    name: "instanceId", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                },
                { 
                    type: "Input",
                    label: "Nginx IP", 
                    name: "nginxIp", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                },
                { 
                    type: "Input",
                    label: "业务", 
                    name: "service", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                },

            ]
        };
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    onFormRef = (ref) => {
        this.child = ref;
    }

    // 关闭弹窗
    handleCancel = () => {
        // 清单表单
        this.child.onReset();
        this.setState({
            isModalVisible: false,  // 关闭弹窗
            formConfig: { setFieldValue: "" },  // 清除form的初始数据
        })
    }
    
    /** 弹窗 */
    visibleModal = (params) => {
        this.setState({
            isModalVisible: params.status,
            slb_id: params.slb_id
        }, () => {
            this.getDetailed();
        })
    }

    /** slb详情 */
    getDetailed = () => {
        if(!this.state.slb_id) { return false; }
        getSlbDataById({id: this.state.slb_id}).then(response => {
            console.log(response)
            const data = response.data;
            this.setState({
                formConfig: {
                    setFieldValue: data
                }
            })
        })
    }

    onBlurEvent = (e) => {
        const value = e.currentTarget.value;
        if(value) {
            this.updateItem(value ? false : true);
            return false;
        }
    }
    // 提交按钮
    submit = (value) => {
        this.state.slb_id ? this.handlerFormEdit(value) : this.handlerFormAdd(value);
    }
    // 添加 提交表单
    handlerFormAdd= (value) => {
        const requestData = value;
        console.log(requestData)
        addSlbData(requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }
    // 编辑 提交表单
    handlerFormEdit= (value) => {
        console.log(value)
        const requestData = value;
        const id = this.state.slb_id;
        updateSlbData(id, requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }

    render(){
        return (
            <Modal title="详情" visible={this.state.isModalVisible} footer={null} onCancel={this.handleCancel}>
                <FormCom onRef={this.onFormRef} onBlur={this.onBlurEvent} formItem={this.state.formItem} formLayout={this.state.formLayout} formConfig={this.state.formConfig} submit={this.submit}>
                </FormCom>
            </Modal>
        )
    }
}

export default SlbModal;